import { getSelfSignInfoReq } from './sign.service'
import { getQuestionnaireListReq } from './questionnaire.service'
import { getIdeaShareStatusReq } from './ideaShare.service'
import { showText } from '../../utils/util';
/** 获取首页四个任务状态 */
export const getTaskStatus = async () => {
    await getSignStatus();
    await getQuestionnaireStatus();
    await getIdeaShareStatus();
    console.log("finish PointTaskList is", PointTaskList);
    return PointTaskList;
}

/** 获取任务1-签到状态 */
const getSignStatus = async () => {
    return getSelfSignInfoReq().then(res => {
        if (res.code == 200) PointTaskList[0].status = Boolean(res.data);
        else {
            showText(res.msg);
            throw new Error("request fail：" + res.msg);
        }
    })
}

/** 获取任务2-问卷状态 */
const getQuestionnaireStatus = async () => {
    return getQuestionnaireListReq().then((res) => {
        if (res.code == 200) {
            const qData = res.rows.sort((a, b) => b.aeId - a.aeId)[0]; //获取id最大的问卷
            if (qData) {
                PointTaskList[1].status = qData.answered;
                PointTaskList[1].url = qData.answered ? '' : `/pages/questionnaire/questionnaire?id=${qData.aeId}`;
            }
            else PointTaskList[1].url = ''
        } else {
            showText(res.msg);
            throw new Error("request fail：" + res.msg);
        }
    })
}

/** 获取任务3-护理提醒状态  */
const getTask3Status = () => {
    //TODO
}

/** 获取任务4-心得分享状态 */
const getIdeaShareStatus = async () => {
    return getIdeaShareStatusReq().then((res) => {
        if (res.code == 200) {
            PointTaskList[3].status = !res.data.todayCanSubmit;
            PointTaskList[3].url = res.data.todayCanSubmit ? '/pages/ideaShare/form/form' : '/pages/ideaShare/audit/audit';
            if (res.data.appExperience?.status == 1) PointTaskList[3].stateTip = '审核中';
        }
        else {
            showText(res.msg);
            throw new Error("request fail：" + res.msg);
        }
    })
}

// export const TaskConfig = {
//     defaultBtnName:'立即参与',
//     defaultStateTip:'未完成',
// }

/** 首页弹窗任务列表 */
export const PointTaskList = [
    {
        name: "每日签到",
        status: false,
        btnName: "立即签到",
        btnNameOK: '每日提醒',
        // statusConfig: {
        //     0: {
        //         btnName: '立即签到',
        //     },
        //     1: {
        //         btnName: '每日提醒',
        //     }
        // }
    },
    {
        name: "有奖问卷",
        status: false,
        btnName: "立即参与",
        btnNameOK: '立即参与',
        noUrlTip: '已完成问卷'
    },
    {
        name: "护理提醒",
        status: false,
        btnName: "绑定设备",
        btnNameOK: '绑定设备',
        url: ""
    },
    {
        name: "心得分享",
        status: false,
        stateTip: '',
        btnName: "立即提交",
        btnNameOK: '审核记录',
    }
]